package com.softtown.modules.dapai.service.impl;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softtown.common.enums.IsDeleteEnum;
import com.softtown.common.service.impl.BaseServiceImpl;
import com.softtown.common.utils.ConvertUtils;
import com.softtown.common.utils.TreeUtils;
import com.softtown.modules.dapai.dao.CompCityDao;
import com.softtown.modules.dapai.dao.CompSaleOrderDao;
import com.softtown.modules.dapai.dto.CompCityDTO;
import com.softtown.modules.dapai.dto.CompSaleOrderPageDTO;
import com.softtown.modules.dapai.dto.SaleOrderAddOrUpdateDTO;
import com.softtown.modules.dapai.entity.*;
import com.softtown.modules.dapai.service.CompCityService;
import com.softtown.modules.dapai.service.CompSaleOrderDetailService;
import com.softtown.modules.dapai.service.CompSaleOrderService;
import com.softtown.modules.dapai.vo.CompCityVO;
import com.softtown.modules.dapai.vo.CompSaleOrderVO;
import com.softtown.modules.dapai.vo.SaleOrderCustomerVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


@Service
public class CompSaleOrderServiceImpl extends
        BaseServiceImpl<CompSaleOrderDao, CompSaleOrderEntity> implements CompSaleOrderService {


    @Resource
    private CompSaleOrderDetailService compSaleOrderDetailService;

    @Override
    public PageInfo<CompSaleOrderEntity> getSaleOrderPageList(CompSaleOrderPageDTO compSaleOrderPageDTO) {
        Integer pageSize = compSaleOrderPageDTO.getPageSize();
        Integer page = compSaleOrderPageDTO.getPage();
        if (pageSize > 0 && page > 0) {
            PageHelper.startPage(page, pageSize);
        }

        List<CompSaleOrderEntity>  compSaleOrderEntities =
                baseDao.getSaleOrderPageList(compSaleOrderPageDTO.getCompId(), compSaleOrderPageDTO.getParam());
        return new PageInfo<>(compSaleOrderEntities);
    }

    @Override
    public CompSaleOrderVO getSaleOrderById(Long compId, Long id) {
        CompSaleOrderVO compSaleOrderVO  = new CompSaleOrderVO();
        compSaleOrderVO.setCompSaleOrderEntity(baseDao.getSaleOrderById(compId, id));
        compSaleOrderVO.setCompSaleOrderDetailEntities(baseDao.getOrderDetailById(compId, id));
        return compSaleOrderVO;
    }

    @Override
    @Transactional
    public void addSaleOrder(SaleOrderAddOrUpdateDTO saleOrderAddOrUpdateDTO) {
            CompSaleOrderEntity compSaleOrderEntity =ConvertUtils
                .sourceToTarget(saleOrderAddOrUpdateDTO.getCompSaleOrderDTO(), CompSaleOrderEntity.class);
        insert(compSaleOrderEntity);
        List<CompSaleOrderDetailEntity> compSaleOrderDetailEntities = ConvertUtils
                .sourceToTarget(saleOrderAddOrUpdateDTO.getCompSaleOrderDetailDTOList(), CompSaleOrderDetailEntity.class);
        compSaleOrderDetailEntities.forEach(item ->{
            item.setCompId(compSaleOrderEntity.getCompId());
            item.setIsDeleted(IsDeleteEnum.NORMAL.value());
            item.setSaleOrderId(compSaleOrderEntity.getId());
        });
        compSaleOrderDetailService.insertBatch(compSaleOrderDetailEntities);
    }

    @Override
    @Transactional
    public void updateSaleOrder(SaleOrderAddOrUpdateDTO saleOrderAddOrUpdateDTO) {
        CompSaleOrderEntity compSaleOrderEntity =ConvertUtils
                .sourceToTarget(saleOrderAddOrUpdateDTO.getCompSaleOrderDTO(), CompSaleOrderEntity.class);
        updateById(compSaleOrderEntity);
        List<CompSaleOrderDetailEntity> compSaleOrderDetailEntities = compSaleOrderDetailService.list(new QueryWrapper<CompSaleOrderDetailEntity>()
        .lambda().eq(CompSaleOrderDetailEntity::getSaleOrderId,compSaleOrderEntity.getId()));
        compSaleOrderDetailEntities.forEach(item->{
            item.setIsDeleted(IsDeleteEnum.DELETE.value());
        });
        compSaleOrderDetailService.updateBatchById(compSaleOrderDetailEntities);

        List<CompSaleOrderDetailEntity> compSaleOrderDetailEntities1 = ConvertUtils
                .sourceToTarget(saleOrderAddOrUpdateDTO.getCompSaleOrderDetailDTOList(), CompSaleOrderDetailEntity.class);
        compSaleOrderDetailEntities1.forEach(item ->{
            item.setIsDeleted(IsDeleteEnum.NORMAL.value());
            item.setSaleOrderId(compSaleOrderEntity.getId());
        });
        compSaleOrderDetailService.insertBatch(compSaleOrderDetailEntities1);
    }

    @Override
    @Transactional
    public void deleteSaleOrder(Long compId, Long id) {
        List<CompSaleOrderDetailEntity> compSaleOrderDetailEntities = compSaleOrderDetailService.list(new QueryWrapper<CompSaleOrderDetailEntity>()
                .lambda().eq(CompSaleOrderDetailEntity::getSaleOrderId,id));
        compSaleOrderDetailEntities.forEach(item->{
            item.setIsDeleted(IsDeleteEnum.DELETE.value());
        });
        compSaleOrderDetailService.updateBatchById(compSaleOrderDetailEntities);
        CompSaleOrderEntity compSaleOrderEntity = baseDao.selectById(id);
        compSaleOrderEntity.setIsDeleted(IsDeleteEnum.DELETE.value());
        updateById(compSaleOrderEntity);
    }

}
